1 package net.sourceforge.selfesteem;
2
3 import org.jdom.JDOMException;
4
5 import java.io.*;
6
7 public class Serializer {
8 private static final String DASHES = "---------------------------------------";
9 private int _lineCount;
10 private StringBuffer _lines = new StringBuffer();
11 private static final String REPLACE_TOKEN = "{{{lines}}}";
12
13 public void addLine(int level, String line) {
14 addLine(DASHES.substring(0, level) + escape(line));
15 }
16
17 protected void addLine(String line) {
18 _lineCount++;
19 _lines.append(" line" + _lineCount + "='" + line + "'\n");
20 }
21
22 private String escape(String text) {
23 StringBuffer buffer = new StringBuffer(text);
24 for(int i = 0; i < buffer.length(); i++) {
25 switch (buffer.charAt(i)) {
26 case '<':
27 i = replace(buffer, i, "<");
28 break;
29 case '>':
30 i = replace(buffer, i, ">");
31 break;
32 case '&':
33 i = replace(buffer, i, "&");
34 break;
35 case '\'':
36 i = replace(buffer, i, "'");
37 break;
38 case '"':
39 i = replace(buffer, i, """);
40 break;
41 }
42 }
43 return buffer.toString();
44 }
45
46 private int replace(StringBuffer buffer, int i, String code) {
47 buffer.replace(i, i + 1, code);
48 return i + code.length();
49 }
50
51 public void serialize(Node node) {
52 _lineCount = 0;
53 _lines = new StringBuffer();
54 node.serialize(this, 0);
55 }
56
57 public String toString() {
58 return "lines='" + _lineCount + "'\n" + _lines;
59 }
60
61 private void merge(File template, File file) throws IOException {
62 BufferedReader in = new BufferedReader(new FileReader(template));
63 BufferedWriter out = new BufferedWriter(new FileWriter(file));
64
65 boolean foundToken = false;
66 String line = null;
67 while ((line = in.readLine()) != null) {
68 if (!foundToken && line.indexOf(REPLACE_TOKEN) != -1) {
69 foundToken = true;
70 int i = line.indexOf(REPLACE_TOKEN);
71 line = line.substring(0, i) + toString() + line.substring(i + REPLACE_TOKEN.length());
72 }
73 out.write(line);
74 out.write('\n');
75 }
76
77 if (!foundToken) {
78 throw new IOException("didn't find token " + REPLACE_TOKEN + " in template file " + template);
79 }
80
81 out.close();
82 in.close();
83 }
84
85 public static void main(String[] args) throws IOException, JDOMException {
86 File outputTemplateFile = new File(args[0]);
87 File outputFile = new File(args[1]);
88 File storyFile = new File(args[2]);
89 File[] testResults = new File[args.length - 3];
90 for (int i = 3; i < args.length; i++) {
91 testResults[i - 3] = new File(args[i]);
92 }
93
94 StoryParser parser = new StoryParser();
95 parser.parseStoryFile(storyFile);
96 for (int i = 0; i < testResults.length; i++) {
97 parser.parseTestResults(testResults[i]);
98 }
99
100 Serializer serializer = new Serializer();
101 serializer.serialize(parser.getRoot());
102 serializer.merge(outputTemplateFile, outputFile);
103 }
104 }
This page was automatically generated by Maven